﻿using System;
using System.Collections.Generic;
using System.Text;
using OT.ORM;
using System.Data.Common;
using System.ComponentModel;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.Xpo.Metadata;

namespace OT.Dev
{
    public class BaseBLL<E,T> : IObjectBLL
        where E : XPLiteObject , new() 
        where T : new()
    {
        #region XpoSession

        Session _xpoSession;

        public Session XpoSession
        {
            get { return _xpoSession; }
            set { _xpoSession = value; }
        }
        #endregion

        #region AssertSession

        private void AssertSession()
        {
            if (XpoSession == null)
                throw new ApplicationException("Session不能为空");
        }
        #endregion

        #region _DeleteByKey

        virtual public void _DeleteByKey(string id)
        {
            AssertSession();
            E entity = XpoSession.GetObjectByKey<E>(id);
            if (entity != null)
                entity.Delete();
        }
        #endregion

        #region _DeleteByKeys

        virtual public void _DeleteByKeys(List<string> ids)
        {
            if (ids.Count == 0)
                return;

            AssertSession();

            EntityMeta<E> meta = new EntityMeta<E>(XpoSession);

            XPCollection<E> xp = new XPCollection<E>(XpoSession, new InOperator(meta.KeyProperty, ids));
            XpoSession.Delete(xp);
        }

        #endregion

        #region _CheckDelete

        virtual public bool _CheckDelete(string id, ref string error)
        {
            throw new ApplicationException("必须重载该函数进行删除校验");
            return true;
        }

        #endregion

    }
}
